﻿#include "../HSettingPanel.h"
#include "../HCustomWidgets/HComponent.h"
#include "../../include/StdAfx.h"
#include "../../include/HGlobalVariable.h"
#include "../../include/HSettingConfigOperator.h"
#include "../HCustomWidgets/HExitPanel.h"

HSettingPanel::HSettingPanel(QWidget *parent)
    :QWidget(parent)
{
    bool t = HSettingConfigOperator::readConfig(m_hashConfigNameToValue,g_strAppPath + XMLRELATIONPATH + "settingConfig.xml");
    initControlSize();
    initUI();
}

HSettingPanel::~HSettingPanel()
{

}

void HSettingPanel::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    QWidget::paintEvent(event);
}

void HSettingPanel::initControlSize()
{
    m_nLayoutSpacing = 15;
    m_nMainLayoutL = 30;
    m_nMainLayoutU = 25;
    m_nMainLayoutR = 30;
    m_nMainLayoutB = 40;
    m_nSubMainLayoutL = 22;
    m_nSubMainLayoutU = 0;
    m_nSubMainLayoutR = 22;
    m_nSubMainLayoutB = 0;
}

void HSettingPanel::initUI()
{
    m_hashDataToLineEdit.clear();
    this->setObjectName("login_wnd_mainPanel");

    m_ppbMin = new QPushButton(this);
    m_ppbMin->setFixedSize(NLOGINPANELCONTROLBTNWIDTH,NLOGINPANELCONTROLBTNWIDTH);
    m_ppbMin->setObjectName("login_btn_min");

    m_ppbClose = new QPushButton(this);
    m_ppbClose->setFixedSize(NLOGINPANELCONTROLBTNWIDTH,NLOGINPANELCONTROLBTNWIDTH);
    m_ppbClose->setObjectName("lgoin_btn_close");

    m_ppbSave = new QPushButton(this);
    m_ppbSave->setFixedSize(NSAVEBTNWIDTH,NSAVEBTNHEIGHT);
    m_ppbSave->setObjectName("setting_btn_save");
    m_ppbSave->setText(QString::fromLocal8Bit("保存"));

    m_ppbCancel = new QPushButton(this);
    m_ppbCancel->setFixedSize(NSAVEBTNWIDTH,NSAVEBTNHEIGHT);
    m_ppbCancel->setObjectName("setting_btn_cancel");
    m_ppbCancel->setText(QString::fromLocal8Bit("取消"));

    m_pvblMain = new QVBoxLayout();
    m_pvblMain->setContentsMargins(m_nMainLayoutL,m_nMainLayoutU,m_nMainLayoutR,m_nMainLayoutB);
    m_pvblMain->setSpacing(m_nLayoutSpacing);

    m_pvblSubMain = new QVBoxLayout();
    m_pvblSubMain->setSpacing(m_nLayoutSpacing);
    m_pvblSubMain->setContentsMargins(m_nSubMainLayoutL,m_nSubMainLayoutU,m_nSubMainLayoutR,m_nSubMainLayoutB);

    m_phblPanelControl = new QHBoxLayout();
    m_phblPanelControl->setSpacing(0);
    m_phblPanelControl->setContentsMargins(0,0,0,0);

    m_phblFunctionBtns = new QHBoxLayout();
    m_phblFunctionBtns->setSpacing(20);
    m_phblFunctionBtns->setContentsMargins(0,0,0,0);

    m_phblPanelControl->addStretch(100);
    m_phblPanelControl->addWidget(m_ppbMin);
    m_phblPanelControl->addWidget(m_ppbClose);

    m_phblFunctionBtns->addStretch(100);
    m_phblFunctionBtns->addWidget(m_ppbSave);
    m_phblFunctionBtns->addWidget(m_ppbCancel);

    createComponent(QString::fromLocal8Bit("服务器IP地址"),"serverIP");
    createComponent(QString::fromLocal8Bit("服务器端口"),"serverPort");
  //  createComponent(QString::fromLocal8Bit("Sip from"),"sipFrom");
    createComponent(QString::fromLocal8Bit("Sip to"),"sipTo");
    createComponent(QString::fromLocal8Bit("Sip 域"),"sipArea");
    m_pvblSubMain->addLayout(m_phblFunctionBtns);

    m_pvblMain->addLayout(m_phblPanelControl);
    m_pvblMain->addLayout(m_pvblSubMain);
    this->setLayout(m_pvblMain);

    connect(m_ppbSave,SIGNAL(clicked()),this,SIGNAL(sigSaveBtnClicked()));
	connect(m_ppbSave,SIGNAL(clicked()),this,SLOT(sltSave()));
    connect(m_ppbCancel,SIGNAL(clicked()),this,SIGNAL(sigCancelBtnClicked()));

	connect(m_ppbMin,SIGNAL(clicked()),this,SIGNAL(sigMinBtnClicked()));
    //connect(m_ppbClose,SIGNAL(clicked()),HExitPanel::instance(),SLOT(show()));
    connect(m_ppbClose,SIGNAL(clicked()),this,SLOT(sltCloseBtnClicked()));
}

void HSettingPanel::createComponent(QString strTitle, QString strData)
{
    QLabel *t_plabel = new QLabel(this);
    t_plabel->setText(strTitle);
    t_plabel->setObjectName("login_setting_lbl");

    HLineEdit *t_plineEdit = new HLineEdit(this);
    t_plineEdit->setObjectName(strData);
    t_plineEdit->setFixedHeight(NCONTROLSETTINGHEIGHT);
    QString strLineEditText = m_hashConfigNameToValue.value(strData);
    t_plineEdit->setText(strLineEditText);
    t_plineEdit->setStyleSheet(QString("font-family:'微软雅黑';\
                               font-size:12px;\
                               color:#ffffff;\
							   border-image:url(%1HIDI_LOGIN_LINEEDIT_NOR.png);\
							   padding:10px;").arg(g_strAppPath + g_strImageRelationPath));


    QVBoxLayout *t_pvbl = new QVBoxLayout();
    t_pvbl->setSpacing(10);
    t_pvbl->setContentsMargins(0,0,0,0);

    t_pvbl->addWidget(t_plabel,1,Qt::AlignLeft);
    t_pvbl->addWidget(t_plineEdit);

    m_pvblSubMain->addLayout(t_pvbl);

    m_lstlblComponents<<t_plabel;
    m_hashDataToLineEdit.insert(strData,t_plineEdit);
    m_lstvblComponents<<t_pvbl;
    connect(t_plineEdit,SIGNAL(sigFocusStateChanged(bool)),this,SLOT(sltLineEditFocusStateChanged(bool)));
}


void HSettingPanel::sltLineEditFocusStateChanged(bool bHasFocus)
{
    QObject *t_pobjSender = QObject::sender();
    HLineEdit *t_pLineEdit = dynamic_cast<HLineEdit*>(t_pobjSender);
    if(t_pLineEdit)
    {
        if(bHasFocus)
        {
            t_pLineEdit->setStyleSheet(QString("font-family:'微软雅黑';\
                                               font-size:12px;\
                                               color:#ffffff;\
                                               border-image:url(%1HIDI_LOGIN_LINEEDIT_HOR.png);\
											   padding:10px;").arg(g_strAppPath + g_strImageRelationPath));
           // t_pLineEdit->setStyleSheet("background-color:red");border-image:url(%1HIDI_CAPTION_CLOSE_HOR.png);
        }
        else
        {
            t_pLineEdit->setStyleSheet(QString("font-family:'微软雅黑';\
                                               font-size:12px;\
                                               color:#ffffff;\
											   border-image:url(%1HIDI_LOGIN_LINEEDIT_NOR.png);\
											   padding:10px;").arg(g_strAppPath + g_strImageRelationPath));
            //t_pLineEdit->setStyleSheet("background-color:green");
        }

    }
}

void HSettingPanel::sltSave()
{
    QHash<QString,QString> t_hashSettingInfo;
    bool t = HSettingConfigOperator::readConfig(t_hashSettingInfo,g_strAppPath + XMLRELATIONPATH + "settingConfig.xml");
    QList<QString> lstKeys = m_hashDataToLineEdit.keys();
    for(int i = 0; i < lstKeys.count(); i ++)
    {
        t_hashSettingInfo.remove(lstKeys.at(i));
        t_hashSettingInfo.insert(lstKeys.at(i),m_hashDataToLineEdit.value(lstKeys.at(i))->text());
    }

    HSettingConfigOperator::writeConfig(t_hashSettingInfo,"settingConfig.xml",g_strAppPath + XMLRELATIONPATH);
}

void HSettingPanel::sltCloseBtnClicked()
{
    HExitPanel *t_pPanel = new HExitPanel();
    t_pPanel->show();
}
